﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ocean.OceanFramework.WPF;
using WPFLOBMVVM.ViewModel;

namespace WPFLOBMVVM.View
{
    /// <summary>
    /// Interaction logic for EditContactsAjaxComboBoxSelectionView.xaml
    /// </summary>
    public partial class EditContactsAjaxComboBoxSelectionView : UserControl
    {
        public EditContactsAjaxComboBoxSelectionView()
        {
            InitializeComponent();
            this.Loaded +=new RoutedEventHandler(EditContactsAjaxComboBoxSelectionView_Loaded);
            this.Unloaded +=new RoutedEventHandler(EditContactsAjaxComboBoxSelectionView_Unloaded);
            this.cboLastNameSearch.RecordSelected +=new RecordSelectedRoutedEventHandler(cboLastNameSearch_RecordSelected);
            this.cboLastNameSearch.LoadItemsSource +=new LoadItemsSourceRoutedEventHandler(cboLastNameSearch_LoadItemsSource);
        }

        private EditContactsAjaxComboBoxSelectionViewModel _objEditContactsAjaxComboBoxSelectionViewModel;

        private void _objEditContactsAjaxComboBoxSelectionViewModel_ClearAjaxComboBox(object sender, System.EventArgs e)
        {

            if (this.cboLastNameSearch != null)
            {
                this.cboLastNameSearch.ClearAll();
            }

        }

        /// <summary> 
        /// this is a custom event fired by our AjaxStyleComboBox 
        /// it's requesting new data so we provide it 
        /// </summary> 
        private void cboLastNameSearch_LoadItemsSource(object sender, LoadItemsSourceRoutedEventArgs e)
        {

            if (_objEditContactsAjaxComboBoxSelectionViewModel != null)
            {
                this.cboLastNameSearch.ItemsSource = _objEditContactsAjaxComboBoxSelectionViewModel.Search(e.SearchString);
            }

        }

        /// <summary> 
        /// this is a custom event fired by our AjaxStyleComboBox 
        /// a record has been selected, now it's up to the local 
        /// code to act upon a record selection 
        /// </summary> 
        private void cboLastNameSearch_RecordSelected(object sender, RecordSelectedRoutedEventArgs e)
        {

            if (_objEditContactsAjaxComboBoxSelectionViewModel != null)
            {

                if (_objEditContactsAjaxComboBoxSelectionViewModel.VerifyDataBeforeClosing())
                {
                    _objEditContactsAjaxComboBoxSelectionViewModel.Select((int)e.Value);
                    Keyboard.Focus(this.objContactView.txtFirstName);
                }

                else
                {
                    this.cboLastNameSearch.ClearAll();
                }
            }


        }

        private void EditContactsAjaxComboBoxSelectionView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _objEditContactsAjaxComboBoxSelectionViewModel = this.DataContext as EditContactsAjaxComboBoxSelectionViewModel;
            _objEditContactsAjaxComboBoxSelectionViewModel.ClearAjaxComboBox += new EditContactsAjaxComboBoxSelectionViewModel.ClearAjaxComboBoxEventHandler(_objEditContactsAjaxComboBoxSelectionViewModel_ClearAjaxComboBox);
        }

        private void EditContactsAjaxComboBoxSelectionView_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _objEditContactsAjaxComboBoxSelectionViewModel = null;
        } 
    }
}
